package com.tools.s7.delegate.base;

import android.os.Bundle;
import android.view.Menu;

import com.tools.s7.base.CompatActivity;
import com.tools.s7.delegate.ADelegate;

import java.lang.reflect.ParameterizedType;


public abstract class ActivityPresenter<T extends ADelegate> extends CompatActivity {

    public T viewDelegate;


    public ActivityPresenter() {
        ParameterizedType pt = (ParameterizedType) this.getClass()
                .getGenericSuperclass();
        Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];

        try {
            viewDelegate = clazz.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException("create IDelegate error");
        } catch (IllegalAccessException e) {
            throw new RuntimeException("create IDelegate error");
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewDelegate.onCreate(getLayoutInflater(), savedInstanceState);
        setContentView(viewDelegate.getRootView());
        viewDelegate.initWidget();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (viewDelegate.getOptionsMenuId() != 0) {
            getMenuInflater().inflate(viewDelegate.getOptionsMenuId(), menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewDelegate = null;
    }
}
